Imports CrystalDecisions.Shared
Imports CrystalDecisions.CrystalReports.Engine

Partial Class Reportes_Controls_ctlCrReporteAltas
    Inherits System.Web.UI.UserControl
   

    Protected Sub btnRefrescar_Click(ByVal sender As Object, ByVal e As System.EventArgs)
        Dim se As String = ""
        If Validar(se) = True Then
            BindReport()
        Else
            lblMensaje.ClientVisible = True
            lblMensaje.Text = se
        End If
    End Sub

    Protected Sub chkIncludeDateRange_CheckedChanged(ByVal sender As Object, ByVal e As System.EventArgs)
        If chkIncludeDateRange.Checked = True Then
            Me.txtStartDate.Enabled = True
            Me.txtEndDate.Enabled = True
            'Me.txtRange.Text = "1"
        Else
            Me.txtStartDate.Enabled = False
            Me.txtEndDate.Enabled = False
            'Me.txtRange.Text = "0"
        End If
    End Sub

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If Not IsPostBack Then
            chkIncludeDateRange.Checked = True
            Me.txtStartDate.Value = Now().Date
            Me.txtEndDate.Value = Now().Date
        End If
        Me.txtStartDate.MaxDate = DateTime.Today
        Me.txtStartDate.MinDate = "01/01/2011"
        Me.txtEndDate.MaxDate = DateTime.Today
        Me.txtEndDate.MinDate = "01/01/2011"

        BindReport()

    End Sub

    Private Sub BindReport()

        'If Session("rptAltas") Is Nothing Then
        Dim ConnInfo As New ConnectionInfo
        With ConnInfo
            .ServerName = DBUtilities.GetConnectionStringDataSource
            .DatabaseName = DBUtilities.GetConnectionStringDatabaseName
            .UserID = DBUtilities.GetConnectionStringUserID
            .Password = DBUtilities.GetConnectionStringPassWord
        End With

        Dim rep As New ReportDocument
        rep.Load(Server.MapPath("~/Reportes/arcReportes/rptReporteAltasSocios.rpt"))
        Me.CrystalReportViewer1.ReportSource = rep
        Dim RepTbls As Tables = rep.Database.Tables
        For Each RepTbl As Table In RepTbls
            Dim RepTblLogonInfo As TableLogOnInfo = RepTbl.LogOnInfo
            RepTblLogonInfo.ConnectionInfo = ConnInfo
            RepTbl.ApplyLogOnInfo(RepTblLogonInfo)
        Next
        Session("rptAltas") = rep
        'End If

        Me.CrystalReportViewer1.ReportSource = Session("rptAltas")
        If Me.chkIncludeDateRange.Checked = True Then
            Me.CrystalReportViewer1.SelectionFormula = "DateValue ({viewReporteAltasSocios.fechaAprobacion})  >=  DateValue (#" + Format(txtStartDate.Value, "MM/dd/yyyy") + "#) AND DateValue ({viewReporteAltasSocios.fechaAprobacion})   <= DateValue (#" + Format(txtEndDate.Value, "MM/dd/yyyy") + "#)"
        Else
            Me.CrystalReportViewer1.SelectionFormula = ""
        End If

        Me.CrystalReportViewer1.DataBind()
    End Sub

    Protected Sub txtStartDate_DateChanged(ByVal sender As Object, ByVal e As System.EventArgs)

    End Sub

    Protected Sub txtEndDate_DateChanged(ByVal sender As Object, ByVal e As System.EventArgs)

    End Sub

    Protected Sub CrystalReportViewer1_Init(ByVal sender As Object, ByVal e As System.EventArgs)

    End Sub

    Function Validar(ByRef sError As String) As Boolean
        Validar = False
        lblMensaje.ClientVisible = False
        If Me.txtStartDate.Value > Now() Then
            sError = "Fecha Inicial no puede ser mayor a la actual"
            Exit Function
        End If
        If Me.txtEndDate.Value > Now() Then
            sError = "Fecha Final no puede ser mayor a la actual"
            Exit Function
        End If
        If Me.txtEndDate.Value > Me.txtStartDate.Date Then
            sError = "Fecha Final no puede ser mayor a la Fecha inicial"
            Exit Function
        End If

        Validar = True

    End Function

End Class
